﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_Tintuc : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.Title = "Tin tức mới nhất";
            //rptTintuc.DataSource = LayTinTuc();
            //rptTintuc.DataBind();
            loadData();

        }
    }
    public string Url(string id)
    {
        return "ChitietTintuc.aspx?ID=" + id;
    }
    BUS_Tintuc tintuc = new BUS_Tintuc();
    public void loadData()
    {
        DataTable dt = new DataTable();

        dt = tintuc.LayTinTuc();
        // Starting paging here.
        PagedDataSource pds = new PagedDataSource();
        DataView dv = new DataView(dt);
        pds.DataSource = dv;
        pds.AllowPaging = true;
        // Show number of product in one page.
        pds.PageSize = 2;
        // Specify sum of page.
        int numPage = pds.PageCount;

        int currentPage = 0;
        if (Request.QueryString["page"] != null)
        {
            try
            {

                currentPage = Int32.Parse(Request.QueryString["page"]);
            }
            catch
            {
                Response.Redirect(Request.CurrentExecutionFilePath);
            }
        }
        else
        {
            currentPage = 1;
        }
        // Because paging always start at 0.
        if (currentPage >0 && currentPage <= numPage)
        {
            pds.CurrentPageIndex = currentPage - 1;
        }
        else
        {
            Response.Redirect(Request.CurrentExecutionFilePath);
        }
        // Show
        Label1.Text = "Page " + currentPage + " of " + pds.PageCount;


        lnkCurrent.Text = Convert.ToString(currentPage);
        if (currentPage==1)
        {
            Page.Title += "Tin tức mới nhất";
        }
        else
            Page.Title += "Tin tức - Trang " + currentPage;
        
        if (!pds.IsFirstPage)
        {
            lnkPre.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage - 1);
            lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath;
            if (currentPage - 1 > 0)
            {
                lnkCurrent_1.Text = Convert.ToString(currentPage - 1);
                lnkCurrent_1.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage - 1);
            }
            else lnkCurrent_1.Visible = false;

            if (currentPage - 1 > 1)
            {
                lnkCurrent_2.Text = Convert.ToString(currentPage - 2);
                lnkCurrent_2.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage - 2);
            }
            else lnkCurrent_2.Visible = false;
            if (currentPage - 1 > 2)
            {
                lnkCurrent_3.Text = Convert.ToString(currentPage - 3);
                lnkCurrent_3.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage - 3);
            }
            else lnkCurrent_3.Visible = false;
            
        }
        else
        {
            lnkFirst.Visible = false;
            lnkPre.Visible = false;
            lnkCurrent_1.Visible = false;
            lnkCurrent_2.Visible = false;
            lnkCurrent_3.Visible = false;
        }
        if (!pds.IsLastPage)
        {

            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 1);
            lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(numPage);
            if (currentPage + 1 <= numPage)
            {
                lnkCurrent1.Text = Convert.ToString(currentPage + 1);
                lnkCurrent1.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 1);
            }
            else lnkCurrent1.Visible = false;
            if (currentPage + 2 <= numPage)
            {
                lnkCurrent2.Text = Convert.ToString(currentPage + 2);
                lnkCurrent2.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 2);
            }
            else lnkCurrent2.Visible = false;
            if (currentPage + 3 <= numPage)
            {
                lnkCurrent3.Text = Convert.ToString(currentPage + 3);
                lnkCurrent3.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 3);
            }
            else lnkCurrent3.Visible = false;
        }
        else
        {
            lnkLast.Visible = false;
            lnkNext.Visible = false;
            lnkCurrent1.Visible = false;
            lnkCurrent2.Visible = false;
            lnkCurrent3.Visible = false;
        }

        rptTintuc.DataSource = pds;
        rptTintuc.DataBind();
    }
    
}